package com.example.vbloodbanksalesforce;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.vbloodbanksalesforce.bean.BloodDetail;
import com.example.vbloodbanksalesforce.service.HttpUrlConnectionClass;
import com.example.vbloodbanksalesforce.service.Session;

public class DonorDetailsList extends BaseActivity {
	ArrayList<BloodDetail> donardetailslist = new ArrayList<BloodDetail>();
	HttpUrlConnectionClass httpUrl;
	private ProgressDialog mDialog;
	LinearLayout linear;
	ListView lv;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.donardetailslist);
		linear =(LinearLayout) findViewById(R.id.lldetail);
		lv =(ListView) findViewById(R.id.listview);
		linear.setVisibility(View.GONE);
		Intent i = getIntent();
		
		String area = i.getStringExtra("K1");
		//String bloodgroup = i.getStringExtra("K2");
		lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int position,
					long arg3) {
				// TODO Auto-generated method stub
				BloodDetail bean = donardetailslist.get(position);
				String name = "";
				if (bean.getName()!=null) {
					name = bean.getName();
				}
				String age = "";
				if (bean.getDonarage()!=null) {
					age = bean.getDonarage();
				}
				String phno = "";
				if (bean.getDonarphoneno()!=null) {
					phno = bean.getDonarphoneno();
				}
				String email = "";
				if (bean.getEmailid()!=null) {
					email = bean.getEmailid();
				}
				
				Intent i = new Intent(getApplicationContext(),DonarFullDetails.class);
				i.putExtra("k1", name);
				i.putExtra("k2", age);
				i.putExtra("k3", phno);
				i.putExtra("k4", email);
				startActivity(i);
			}
		});
		httpUrl = new HttpUrlConnectionClass(null);
		httpUrl.attach(this);

		String instanceUrl = Session.sInstanceUrl+"/services/data/v26.0/query/?q=Select+Name,DonorAge__c,DonorPhoneNO__c,EmailID__c+from+DonorRegistration__c+where+DonarArea__c='"+area+"'";
		Log.i("NCS","instanceUrl  is  :"+instanceUrl); 
		httpUrl.execute(instanceUrl, "retrive");
		
		
		OnCancelListener onCancelListener = new OnCancelListener() {
			public void onCancel(DialogInterface arg0) {      
				DialogCancelled();
				finish();
			}
		};                   

		mDialog = new ProgressDialog(this);
		mDialog.setMessage("Loading...");
		mDialog.setCanceledOnTouchOutside(false);                           
		mDialog.setIndeterminate(true);                           
		mDialog.setCancelable(true);
		mDialog.setOnCancelListener(onCancelListener);
		mDialog.show();          
	}
	public void DialogCancelled() {                       

		httpUrl.cancel(true);                                 
		
	}
	
	@Override
	public void apiReturn(String response) {
		
		Log.i("NCS","Area  Response is   :"+response);  
		if(response!=null){
			mDialog.dismiss();
			linear.setVisibility(View.VISIBLE);
		}

		donardetailslist.clear();
		try {
			
			JSONObject jobj_1 = new JSONObject(response);
			JSONArray jarray = jobj_1.getJSONArray("records");
			for (int i = 0; i < jarray.length(); i++) {
				BloodDetail bean = new BloodDetail();
				JSONObject jobj_2 = jarray.getJSONObject(i);
				if (!jarray.isNull(i)) {
					if (!jobj_2.isNull("Name")) {
						String name = jobj_2.getString("Name");
						bean.setName(name);
					}
					if (!jobj_2.isNull("DonorAge__c")) {
						String age = jobj_2.getString("DonorAge__c");
						bean.setDonarage(age);
					}
					if (!jobj_2.isNull("DonorPhoneNO__c")) {
						String phonoNo = jobj_2.getString("DonorPhoneNO__c");
						bean.setDonarphoneno(phonoNo);
					}
					if (!jobj_2.isNull("EmailID__c")) {
						String email = jobj_2.getString("EmailID__c");
						bean.setEmailid(email);
					}
					donardetailslist.add(bean);
				}
				
				refresh();
			
			}
			
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void refresh(){
		
		List donorlist = getDonorlist();
		ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.display,donorlist);
		lv.setAdapter(adapter);
	}
	
	private List getDonorlist() {

		List list = new ArrayList();

		for (BloodDetail c : donardetailslist)    

		{

		list.add(c.getName());

		}

		return list;
		}
	
}